<template>
    <view class="workerHome">
        <up-navbar placeholder title="搬搬货">
            <template #left></template>
        </up-navbar>
        <myTips></myTips>
        <signIn :isSign="isSign" :signDay="signDay" @sign="handlerSign"></signIn>
        <view class="advertisement">
            <view class="title flex_l">师傅接单时需要注意</view>
            <view class="content">
                ● 师傅关注公众号可以及时收到接活提醒；
                <br>
                ● 接单后请及时联系客户确认订单信息；
                <br>
                ● 订单完成后请与客户沟通确认订单；
                <br>
                ● 提现时需要完善个人资料以便平台实时转账到银行账户。
            </view>
        </view>
        <!-- <view class="prize">
            <view class="coupon_title">新用户注册抽奖</view>
            <view class="coupon_use" @click="toPrizeDraw">去看看</view>
            <image
                src="https://www.banbanhuo.com/minio/banbanhuo/269e13b3c6983aa19b4d763c830e4e3ebf9183a7affcfe01a6f58fa828bc6756.png" />
        </view> -->
    </view>
</template>

<script setup lang="ts">
import myTips from "@/components/my-tips/my-tips.vue";
import signIn from "../components/signIn.vue";
import { onShow } from "@dcloudio/uni-app";
import { getSign, createSign } from '@/api/workerPage/home'
import { computed, provide, ref } from "vue";
import { useMemberStore } from "@/stores";
import { isSameDay, showLogin } from "@/utils";
import { storeToRefs } from "pinia";
const memberStore = useMemberStore()
const { isLogin, userInfo, checkMemberMpResult } = storeToRefs(memberStore)
provide("checkMemberMpResult", checkMemberMpResult)
onShow(() => {
    uni.hideTabBar()
    // 进入首页后判断是否登录
    if (!isLogin.value) {
        showLogin()
    } else {
        // 获取签到信息
        getSignList()
        memberStore.updateAuthInfo()
         // 检查是否关注公众号
        memberStore.checkMemberMpRes()
    }
})
// 查询签到
const isSign = computed(() => {
    let flag = false
    if (signList.value.length == 0) {
        flag = false
    } else {
        // 获取最后一次签到的时间
        const lastSignData: any = signList.value[0]
        const lastSign = lastSignData?.createTime
        const nowTime = new Date().getTime()
        // 判断是否是同一天
        const isSameDayFlag = isSameDay(lastSign, nowTime)
        flag = isSameDayFlag
    }
    return flag
})
const signDay = computed(() => {
    return signList.value.length
})
const signList = ref([])
const getSignList = async () => {
    const par = {
        userId: userInfo.value?.userId
    }
    const res: any = await getSign(par.userId)
    if (res.code == 0 && res.data?.list) {
        signList.value = res.data.list
    }
}
// 点击签到
const handlerSign = async () => {
    if (isSign.value) {
        uni.showToast({
            title: "今天已经签到过了,请明天再来吧~",
            icon: "none"
        })
        return
    }
    const par = {
        userId: userInfo.value?.userId,
        point: 1
    }
    const res: any = await createSign(par)
    if (res.code == 0 && res.data) {
        uni.showToast({
            title: "签到成功",
            icon: "none"
        })
        getSignList()
    }
}
// 抽奖
const toPrizeDraw = () => {
    uni.navigateTo({
        url: '/pages/other/prizeDraw/prizeDraw'
    })
}
</script>

<style lang="scss" scoped>
.workerHome {
    min-height: 100vh;
    overflow: hidden;
    background: #F8F8F8;
}

.advertisement {
    width: 698rpx;
    background: #FFFFFF;
    border-radius: 16rpx 16rpx 16rpx 16rpx;
    margin: 0 auto;
    margin-top: 24rpx;

    .title {
        width: 698rpx;
        height: 102rpx;
        background: linear-gradient(90deg, #F7D780 0%, #FF6A19 100%);
        font-weight: 700;
        font-size: 30rpx;
        color: #FFFFFF;
        padding: 0 28rpx;
        box-sizing: border-box;
        border-radius: 16rpx 16rpx 0 0;
    }

    .content {
        padding: 24rpx 24rpx 32rpx 24rpx;
        font-size: 28rpx;
        color: #222222;
    }
}

.prize {
    width: 698rpx;
    height: 176rpx;
    background: linear-gradient(270deg, #ffffff 0%, #ffedb8 100%);
    border-radius: 16rpx 16rpx 16rpx 16rpx;
    margin: 0 auto;
    margin-top: 24rpx;
    overflow: hidden;
    padding-left: 34rpx;
    box-sizing: border-box;
    position: relative;

    .coupon_title {
        font-weight: 600;
        font-size: 38rpx;
        color: #fe6e04;
        line-height: 45rpx;
        margin-top: 34rpx;
    }

    .coupon_use {
        width: 130rpx;
        height: 42rpx;
        background: #fe6e04;
        border-radius: 20rpx 20rpx 20rpx 20rpx;
        margin-top: 20rpx;
        font-size: 24rpx;
        color: #ffffff;
        line-height: 42rpx;
        text-align: center;
    }

    image {
        width: 194rpx;
        height: 146rpx;
        position: absolute;
        top: 30rpx;
        right: 54rpx;
    }
}
</style>